package com.minelx.sqlstructures.statements.parse.select.where;

import com.minelx.sequence.core.StringScanner;
import com.minelx.sequence.core.ReturnMatcher;
import com.minelx.sqlstructures.statements.clause.Sql;
import com.minelx.sqlstructures.statements.clause.Conditions;

public class EqMatcher implements ReturnMatcher<Sql, StringScanner> {
	private static final String EQUALS = "=";

	private final ReturnMatcher<String, StringScanner> valuesMatcher;

	public EqMatcher() {
		valuesMatcher = new ValuesMatcher();
	}

	@Override
	public boolean isMatch(StringScanner source) {
		StringScanner copy = source.copy();
		copy.trim();
		copy.nextPage();
		copy.trim();
		return copy.exists(EQUALS);
	}

	@Override
	public Sql onMatched(StringScanner source) {
		String left = source.nextPage();
		source.trim();
		source.goes(EQUALS);
		source.trim();
		String right = valuesMatcher.onMatched(source);
		return Conditions.eq(left, right);
	}
}
